import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'


import Behavior from '../views/Management/Behavior/Behavior.vue'


import Role from '../views/Management/Role/Role.vue'
import EditRole from '../views/Management/Role/editRole.vue'

import Account from '../views/Management/Account/Account.vue'
import AddAcount from '../views/Management/Account/addAccount.vue'
import EditAcount from '../views/Management/Account/editAccount.vue'

import VIPCard from '../views/VIP/VIPCard.vue'
import VIP from '../views/VIP/VIP.vue'
import Consult from '../views/Consult/Consult.vue'

import Commodity from '../views/Commodity/Shop/Commodity.vue'

import ShopManagement from '../views/Commodity/Manage/ShopManagement.vue'
import Calendar from '../views/Commodity/Manage/Calendar.vue'
import RotationChart from '../views/RotationChart/RotationChart.vue'
import Operate from '../views/Operate/Operate.vue'



Vue.use(VueRouter)

const routes =  [
  // 重定向
  {path:'/',redirect:'/login'},
  {path:'/login',component:Login},
  {path:'/register',component:Register},
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/admin_permission_list',component:Behavior},
      {path:'/admin_role_list',component:Role},
      {path:'/edit_role',component:EditRole},
      {path:'/admin_account_list',component:Account},
      {path:'/add_account',component:AddAcount},
      {path:'/edit_account',component:EditAcount},
      {path:'/admin_vip_setting',component:VIPCard},
      {path:'/admin_vip_list',component:VIP},
      {path:'/admin_news_list',component:Consult},
      {path:'/admin_type_list',component:Commodity},
      {path:'/admin_product_list',component:ShopManagement},
      {path:'/calendar',component:Calendar},
      {path:'/admin_picture_list',component:RotationChart},
      {path:'/admin_operation',component: Operate}
    ]
},
]
 

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next) => {
  // 如果从用户访问的登录页面，直接放行
  if(to.path === '/login') return next()
  // 从sessionStorage 中获取保存的token
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有token，强制跳转到登录页面
  if(!tokenStr) return next('/login')
  next()
})

export default router
